package com.platform.comm.common.batch.listener;

import com.platform.batch.base.exception.JobExecuteFailedException;
import com.platform.batch.base.exception.JobNeedSkippedException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.annotation.OnSkipInProcess;
import org.springframework.batch.core.annotation.OnSkipInRead;
import org.springframework.batch.core.annotation.OnSkipInWrite;

/**
 * @author Tommy
 */

@Slf4j
public class CustomSkipListener {

    @OnSkipInRead
    public void onSkipInRead( Throwable t) {
        log.info("process:reading....");

        if (t instanceof Exception) {
            log.error(t.getMessage(), t);
        }
    }

    @OnSkipInWrite
    public void onSkipInWrite(Object item, Throwable t) {
        log.info("process:{}", item);

        if (t instanceof Exception) {
            log.error(t.getMessage(), t);
        }
    }

    /**
     * 在进行数据处理的时候，凡是验证不符合的数据全部抛出异常
     *
     * @param item 出错数据项
     * @param t exception
     */
    @OnSkipInProcess
    public void onSkipInProcess(Object item, Throwable t) {
        log.info("process:{}", item);

        if (t instanceof JobNeedSkippedException) {
            log.error(t.getMessage(), t);
        }

        if (t instanceof JobExecuteFailedException) {
            throw (JobExecuteFailedException) t;
        }
    }

}
